<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"lanqilu.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":true},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#395ca3","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta property="og:type" content="article">
<meta property="og:title" content="数据结构">
<meta property="og:url" content="https://lanqilu.github.io/2020/05/25/408/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/index.html">
<meta property="og:site_name" content="Halo">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://img.whl123456.top/image/image-20200721174653931.png">
<meta property="og:image" content="e:%5CPictures%5Cblogimg%5C%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.assets%5Cimage-20200721215334969.png">
<meta property="article:published_time" content="2020-05-25T13:20:06.000Z">
<meta property="article:modified_time" content="2020-07-24T05:06:01.796Z">
<meta property="article:author" content="Lanqilu">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://img.whl123456.top/image/image-20200721174653931.png">

<link rel="canonical" href="https://lanqilu.github.io/2020/05/25/408/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>数据结构 | Halo</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-161500949-1"></script>
    <script data-pjax>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-161500949-1');
      }
    </script>


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?256151d1651e9d73ec980b2fc69de8f6";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Halo</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">39</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">85</span></a>

  </li>
        <li class="menu-item menu-item-python">

    <a href="/categories/Python/" rel="section"><i class="fa fa-fw fa-code"></i>Python</a>

  </li>
        <li class="menu-item menu-item-java">

    <a href="/categories/Java/" rel="section"><i class="fa fa-fw fa-code"></i>Java</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://lanqilu.github.io/2020/05/25/408/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="http://img.whl123456.top/image/avatar.jpg">
      <meta itemprop="name" content="Lanqilu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Halo">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数据结构
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-05-25 21:20:06" itemprop="dateCreated datePublished" datetime="2020-05-25T21:20:06+08:00">2020-05-25</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-24 13:06:01" itemprop="dateModified" datetime="2020-07-24T13:06:01+08:00">2020-07-24</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <div class="note info no-icon">
            
          </div>

<a id="more"></a>

<hr>
<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>数据结构包括：线性结构和非线性结构。</p>
<p>线性结构</p>
<ul>
<li>线性结构作为最常用的数据结构，其特点是数据元素之间存在<strong>一对一</strong>的线性关系</li>
<li>线性结构有两种不同的存储结构，即顺序存储结构和链式存储结构。<ul>
<li>顺序存储的线性表称为<strong>顺序表</strong>，顺序表中的存储元素是连续的（地址是连续的）</li>
<li>链式存储的线性表称为<strong>链表</strong>，链表中的存储元素不一定是连续的，元素节点中存放数据元素以及相邻元素的地址信息</li>
</ul>
</li>
<li>线性结构常见的有：数组、队列、链表和栈</li>
</ul>
<p>非线性结构</p>
<p>非线性结构包括：二维数组，多维数组，广义表，树结构，图结构</p>
<h2 id="稀疏数组和队列"><a href="#稀疏数组和队列" class="headerlink" title="稀疏数组和队列"></a>稀疏数组和队列</h2><h3 id="稀疏数组"><a href="#稀疏数组" class="headerlink" title="稀疏数组"></a>稀疏数组</h3><p>需求：编写的五子棋程序中，有存盘退出和续上盘的功能，因为该二维数组的很多值是默认值0，因此记录了很多没有意义的数据，可用稀疏数组进行压缩处理</p>
<p>当一个数组中大部分元素为0，或者为同一个值的数组时，可以使用稀疏数组来保存该数组。</p>
<p>稀疏数组的处理方法是：</p>
<ul>
<li>记录数组一共有几行几列，有多少个不同的值</li>
<li>把具有不同值的元素的行列及值记录在一个小规模的数组（稀疏数组）中，从而缩小程序的规模</li>
</ul>
<p><img src="http://img.whl123456.top/image/image-20200721174653931.png" alt="原数组"></p>
<table>
<thead>
<tr>
<th></th>
<th>行（row）</th>
<th>列（column）</th>
<th>值（value）</th>
</tr>
</thead>
<tbody><tr>
<td>[0]</td>
<td>6</td>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>[1]</td>
<td>0</td>
<td>3</td>
<td>22</td>
</tr>
<tr>
<td>[2]</td>
<td>0</td>
<td>6</td>
<td>15</td>
</tr>
<tr>
<td>[3]</td>
<td>1</td>
<td>1</td>
<td>11</td>
</tr>
<tr>
<td>[4]</td>
<td>1</td>
<td>5</td>
<td>17</td>
</tr>
<tr>
<td>[5]</td>
<td>2</td>
<td>3</td>
<td>-6</td>
</tr>
<tr>
<td>[6]</td>
<td>3</td>
<td>5</td>
<td>39</td>
</tr>
<tr>
<td>[7]</td>
<td>4</td>
<td>0</td>
<td>91</td>
</tr>
<tr>
<td>[8]</td>
<td>5</td>
<td>2</td>
<td>28</td>
</tr>
</tbody></table>
<p>[0]行记录行列数和需要记录的数据，之后每行记录位置对应的数据</p>
<p>二维数组转稀疏数组的思路：</p>
<ul>
<li>遍历原始的二维数组，得到有效的数据的个数（sum）。</li>
<li>根据sum创建稀疏数组（<code>sparseArr int[sum+1][3]</code>）</li>
<li>将二维数组的有效数据存入稀疏数组中</li>
</ul>
<p>稀疏数组转原始的二维数组的思路：</p>
<ul>
<li>读取稀疏数组的第一行，根据第一行的数据创建原始的二维数组</li>
<li>读取后几行的数据并赋给原始的二维数组</li>
</ul>
<details>
<summary>示例代码</summary>    

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SparseArray</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//创建一个原始的二维数组11*11</span></span><br><span class="line">        <span class="keyword">int</span>[][] chessArr = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">11</span>][<span class="number">11</span>];</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//0表示没有棋子,1表示黑子,2表示白子</span></span><br><span class="line">        chessArr[<span class="number">1</span>][<span class="number">2</span>] = <span class="number">1</span>;</span><br><span class="line">        chessArr[<span class="number">2</span>][<span class="number">3</span>] = <span class="number">2</span>;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//输出原始二维数组</span></span><br><span class="line">        System.out.println(<span class="string">"原始的二维数组"</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span>[] row : chessArr) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> date : row) &#123;</span><br><span class="line">                System.out.printf(<span class="string">"%d\t"</span>, date);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//二维数组转稀疏数组</span></span><br><span class="line">        <span class="comment">//1.遍原始的二维数组，得到有效数据的个数</span></span><br><span class="line">        <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; chessArr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; chessArr.length; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (chessArr[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    sum++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        System.out.println();</span><br><span class="line">        System.out.println(<span class="string">"sum="</span> + sum);</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//2.根据sum就创建稀疏数组</span></span><br><span class="line">        <span class="keyword">int</span>[][] sparseArr = <span class="keyword">new</span> <span class="keyword">int</span>[sum + <span class="number">1</span>][<span class="number">3</span>];</span><br><span class="line">        <span class="comment">//给稀疏数组赋值</span></span><br><span class="line">        sparseArr[<span class="number">0</span>][<span class="number">0</span>] = chessArr.length;</span><br><span class="line">        sparseArr[<span class="number">0</span>][<span class="number">1</span>] = chessArr.length;</span><br><span class="line">        sparseArr[<span class="number">0</span>][<span class="number">2</span>] = sum;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//3.遍历二维数组的有效数据数据存入到稀疏数组</span></span><br><span class="line">        <span class="comment">//count用于记录第几个非零数据</span></span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; chessArr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; chessArr.length; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (chessArr[i][j] != <span class="number">0</span>) &#123;</span><br><span class="line">                    count++;</span><br><span class="line">                    sparseArr[count][<span class="number">0</span>] = i;</span><br><span class="line">                    sparseArr[count][<span class="number">1</span>] = j;</span><br><span class="line">                    sparseArr[count][<span class="number">2</span>] = chessArr[i][j];</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//4.输出稀疏数组</span></span><br><span class="line">        System.out.println();</span><br><span class="line">        System.out.println(<span class="string">"稀疏数组"</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; sparseArr.length; i++) &#123;</span><br><span class="line">            System.out.printf(<span class="string">"%d\t%d\t%d\t\n"</span>, sparseArr[i][<span class="number">0</span>], sparseArr[i][<span class="number">1</span>], sparseArr[i][<span class="number">2</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">    </span><br><span class="line">        <span class="comment">//稀疏数组恢复成原始的二维数组</span></span><br><span class="line">    </span><br><span class="line">        <span class="comment">//1.先读取稀疏数组的第一行，根据第一行的数据，创建原始的二维数组</span></span><br><span class="line">        <span class="keyword">int</span>[][] reChessArr = <span class="keyword">new</span> <span class="keyword">int</span>[sparseArr[<span class="number">0</span>][<span class="number">0</span>]][sparseArr[<span class="number">0</span>][<span class="number">1</span>]];</span><br><span class="line">        <span class="comment">//2.读取稀疏数组后几行的数据(从第二行开始)，并赋给二维数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; sparseArr.length; i++) &#123;</span><br><span class="line">            reChessArr[sparseArr[i][<span class="number">0</span>]][sparseArr[i][<span class="number">1</span>]] = sparseArr[i][<span class="number">2</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//输出二维数组</span></span><br><span class="line">        System.out.println(<span class="string">"稀疏数组转换后的二维数组"</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span>[] row : reChessArr) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> date : row) &#123;</span><br><span class="line">                System.out.printf(<span class="string">"%d\t"</span>, date);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//<span class="doctag">TODO:</span>后续优化,将数据保存到本地,并读取</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</details>

<h3 id="队列"><a href="#队列" class="headerlink" title="队列"></a>队列</h3><p>队列是一个有序列表，可以用数组或是链表来实现。</p>
<p>遵循先入先出的原则。即：先存入队列的数据，要先取出。后存入的要后取出</p>
<h4 id="数组模拟队列"><a href="#数组模拟队列" class="headerlink" title="数组模拟队列"></a>数组模拟队列</h4><p>队列本身是有序列表，若使用数组的结构来存储队列的数据，则队列数组的声明如下图, 其中 maxSize 是该队列的最大容量。</p>
<p>因为队列的输出、输入是分别从前后端来处理，因此需要两个变量 front及 rear分别记录队列前后端的下标，front 会随着数据输出而改变，而 rear则是随着数据输入而改变</p>
<blockquote>
<p>rear 是队列最后[含]</p>
<p>front 是队列最前元素[不含]</p>
</blockquote>
<p><img src="E:%5CPictures%5Cblogimg%5C%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.assets%5Cimage-20200721215334969.png" alt="队列"></p>
<p>入队(addQueue)步骤：</p>
<ul>
<li>将尾指针往后移<code>rear+1</code>  ，<code>front == rear</code>时队列为空</li>
<li>当尾指针 rear 小于队列的最大下标 maxSize-1，则将数据存入rear所指的数组元素中，否则无法存入数据。<code>rear == maxSize - 1</code>时队列满</li>
</ul>
<h4 id="数组模拟环形队列"><a href="#数组模拟环形队列" class="headerlink" title="数组模拟环形队列"></a>数组模拟环形队列</h4><p>思路分析：</p>
<ul>
<li>front调整为指向队列的第一个元素，初始值为0</li>
<li>rear调整为指向队列的最后一个元素的后一个位置，空出一个空间作为一个约定，初始值为0</li>
<li>当队列满时条件是<code>(rear+1) % maxSize == front</code></li>
<li>当队列为空的条件是<code>rear == front</code></li>
<li>队列中有效的数据的个数为<code>(rear+maxSize-front) % maxSize</code>等价于<code>(rear-front) % maxSize + 1</code></li>
</ul>
<h2 id="链表"><a href="#链表" class="headerlink" title="链表"></a>链表</h2><h3 id="单链表"><a href="#单链表" class="headerlink" title="单链表"></a>单链表</h3><p>头指针，data域和next域</p>
<ul>
<li>链表是以节点的方式来存储，链式存储</li>
<li>每个节点包含data域和next域（指向下一个节点）</li>
<li>链表的各个节点不一定是连续储存</li>
<li>链表分带节点的链表和没有头节点的链表</li>
</ul>
<h4 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h4><p>使用带head头的单向链表实现水浒英雄排行榜管理</p>
<p>完成对英雄人物的增删改查操作</p>
<ul>
<li>第一种方法在添加英雄时，直接添加到链表的尾部</li>
<li>第二种方式在添加英雄时，根据排名将英雄插入到指定位置(如果有这个排名，则添加失败，并给出提示)</li>
</ul>
<blockquote>
<p>head节点：不存放具体的数据，表示单链表头</p>
</blockquote>
<p>不考虑编号添加思路</p>
<ul>
<li>先创建head头节点</li>
<li>每加一个节点，就直接加入到链表的最后</li>
</ul>
<p>遍历：通过一个辅助变量，帮助遍历整个链表</p>
<p>按照编号顺序添加思路</p>
<ul>
<li>找到新添加节点的位置，通过变量辅助，通过遍历</li>
<li>新的节点的next赋值为辅助变量的next</li>
<li>将辅助变量的next指向新的节点</li>
</ul>
<p>删除节点的思路</p>
<ul>
<li>找到待删除节点的前一个节点，temp</li>
<li>将<code>temp.next=temp.next.next</code></li>
<li>被删除的节点不会有其他引用指向，会被Java垃圾回收机制处理</li>
</ul>
<p>查找单链表的倒数第K个节点思路</p>
<ul>
<li>编写一个方法接受head节点，同时接受一个index（表示倒数第index个节点）</li>
<li>先把链表从头到尾遍历，得到链表的总的长度（<code>getLength</code>）</li>
<li>得到size后从链表第一个开始遍历（<code>size-index</code>）个</li>
</ul>
<p>单链表的反转思路</p>
<ul>
<li>定义一个节点（<code>reverseHead=new HeroNode();</code>）</li>
<li>从头到尾遍历原来的链表，每遍历一个节点就将其取出，并放到新的链表的最前端</li>
<li>原来的链表的<code>head.next  = reverseHead.next</code></li>
</ul>
<p>从尾到头打印单链表思路一</p>
<ul>
<li>先将单链表进行反转后再遍历，会破坏原来链表的结构</li>
</ul>
<p>从尾到头打印单链表思路二</p>
<ul>
<li>利用栈，将各个节点压入到栈中，利用栈的先进后出的特点，实现逆序打印</li>
</ul>
<h3 id="双向链表"><a href="#双向链表" class="headerlink" title="双向链表"></a>双向链表</h3><p>头指针、data域、next域和pre域（指向前一个节点）</p>
<p>单向链表的缺点</p>
<ul>
<li>查找只能是一个方向，而双向链表可以是向前或向后查找</li>
<li>单向链表不能自我删除，需要辅助节点</li>
</ul>
<p>遍历的思路：和单向链表一样但可以向前查找也可以向后查找</p>
<p>添加到双向链表的最后思路：</p>
<ul>
<li>先找到双向链表的最后</li>
<li><code>temp.next=newHeroNode</code></li>
<li><code>newHeroNode.pre=temp</code></li>
</ul>
<p>修改思路：和单向链表一样</p>
<p>删除思路：</p>
<ul>
<li>因为是双向链表可以实现自我删除</li>
<li>直接找到要删除的节点</li>
<li>让<code>删除节点.pre.next=删除节点.next</code>和<code>删除节点.next.pre=删除节点.pre</code></li>
</ul>
<h3 id="单向环形链表"><a href="#单向环形链表" class="headerlink" title="单向环形链表"></a>单向环形链表</h3><p>约瑟夫Josephus问题</p>
<p>设编号为1，2，… n的n个人围坐一圈，约定编号为k（1&lt;=k&lt;=n）的人从1开始报数，数到m的那个人出列，它的下一位又从1开始报数，数到m的那个人又出列，依次类推，直到所有人出列为止，由此产生一个出队编号的序列。</p>
<blockquote>
<p>n=5,k=1,m=2时，出队列的顺序为2、4、1、5、3</p>
</blockquote>
<p>  单向环形链表的创建思路：</p>
<ul>
<li>创建第一个节点并用first变量和cur变量指向该节点，并且next指向自己形成环</li>
<li>当每加入下一个节点时，first指针不动，cur指针下移指向这个节点，再让这个节点的next指向first形成环</li>
</ul>
<p>遍历环形链表，先让辅助变量指向first节点，再通过while循环遍历，<code>辅助变量.next==first</code>遍历结束</p>
<p>出队列思路：</p>
<ul>
<li>需要创建辅助变量，指向环形链表的最后的节点</li>
<li></li>
</ul>
<table>
<thead>
<tr>
<th>操作</th>
<th>数组</th>
<th>链表</th>
</tr>
</thead>
<tbody><tr>
<td>读取</td>
<td>O(1)</td>
<td>O(n)</td>
</tr>
<tr>
<td>插入</td>
<td>O(n)</td>
<td>O(1)</td>
</tr>
<tr>
<td>删除</td>
<td>O(n)</td>
<td>O(1)</td>
</tr>
</tbody></table>

    </div>

    
    
    
      


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/05/02/Blog/Typora-PikGO%E6%89%93%E9%80%A0%E5%8D%9A%E5%AE%A2%E7%BC%96%E8%BE%91%E6%B5%81/" rel="prev" title="Typora+PicGO打造博客编辑流">
      <i class="fa fa-chevron-left"></i> Typora+PicGO打造博客编辑流
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/05/25/C/C%E8%AF%AD%E8%A8%80%E7%AC%94%E8%AE%B0/" rel="next" title="C语言学习笔记">
      C语言学习笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#简介"><span class="nav-number">1.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#稀疏数组和队列"><span class="nav-number">2.</span> <span class="nav-text">稀疏数组和队列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#稀疏数组"><span class="nav-number">2.1.</span> <span class="nav-text">稀疏数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列"><span class="nav-number">2.2.</span> <span class="nav-text">队列</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#数组模拟队列"><span class="nav-number">2.2.1.</span> <span class="nav-text">数组模拟队列</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数组模拟环形队列"><span class="nav-number">2.2.2.</span> <span class="nav-text">数组模拟环形队列</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#链表"><span class="nav-number">3.</span> <span class="nav-text">链表</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#单链表"><span class="nav-number">3.1.</span> <span class="nav-text">单链表</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#实例"><span class="nav-number">3.1.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#双向链表"><span class="nav-number">3.2.</span> <span class="nav-text">双向链表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#单向环形链表"><span class="nav-number">3.3.</span> <span class="nav-text">单向环形链表</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Lanqilu"
      src="http://img.whl123456.top/image/avatar.jpg">
  <p class="site-author-name" itemprop="name">Lanqilu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">85</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/lanqilu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lanqilu" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=zqKvoL_noruOqKG2o6_nouCtoaM" title="E-Mail → http:&#x2F;&#x2F;mail.qq.com&#x2F;cgi-bin&#x2F;qm_share?t&#x3D;qm_mailme&amp;email&#x3D;zqKvoL_noruOqKG2o6_nouCtoaM" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lanqilu</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  
<script src="/js/local-search.js"></script>













    <div id="pjax">
  

  

  

    </div>
</body>
</html>
